package web.service;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import web.mapper.UserMapper;
import web.pojo.User;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public Integer count() {
        return userMapper.count();
    }

    /**
     * 登录
     *
     * @param username 帐号
     * @param password 密码
     * @return
     */
    public Long login(String username, String password) {
        Long id = userMapper.findIdByUsernameAndPassword(username, password);
        if (id == null) {
            return -1l;
        }
        return id;
    }

    /**
     * 获取当前登录人员信息
     *
     * @param id 根据id获取请求
     * @return
     */
    public User simpleUserInfo(Long id) {
        // userMapper.existsById(id);
        User user = userMapper.userInfo(id);

        return user;
    }

    public Long isValidUser(Long id) {
        if (!userMapper.existsById(id)) {
            return -1l;
        }

        return 1l;
    }

    public boolean modifyUsernameById(String username, Long id) {
        try {
            userMapper.modifyUserNameById(username, id);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }


    public boolean modifyUserPasswordById(String password, Long id) {
        try {
            userMapper.modifyUserPasswordById(password, id);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
